package au.edu.swin.swinvehicle.managers;

/**
 * VehicleTypeJSFManagedBean
 * @version 1.0
 * @author Viet Bui
 * @lastmodify 2009-05-11
 */
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeDetailedTO;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeManagerException;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeManagerRemote;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;

public class VehicleTypeJSFManagedBean {

    private static Log log = LogFactory.getLog(VehicleTypeJSFManagedBean.class);
    @EJB
    private VehicleTypeManagerRemote vehicleTypeManagerBean;
    private VehicleTypeDetailedTO vehicleType;

    public VehicleTypeJSFManagedBean() {
        this.vehicleType = new VehicleTypeDetailedTO();
    }

    public VehicleTypeDetailedTO getVehicleType() {
        return vehicleType;
    }

    public void setVehicleType(VehicleTypeDetailedTO vehicleType) {
        this.vehicleType = vehicleType;
    }

    public String addVehicleType() {
        try {
            this.vehicleType.setDeleted(Boolean.FALSE);
            this.vehicleTypeManagerBean.addVehicleType(this.vehicleType);
            // TODO: Pass the success message back to view
            return "addVehicleType";
        } catch (Exception e) {
            log.error("Cannot add vehicle type", e);
            // TODO: Pass the error message back to view
            return "error";
        }
    }

    public List<VehicleTypeDetailedTO> getVehicleTypes() {
        List<VehicleTypeDetailedTO> vehicleTypes = new ArrayList<VehicleTypeDetailedTO>();
        try {
            vehicleTypes = this.vehicleTypeManagerBean.getAllDetailedVehicleTypes();
        } catch (Exception e) {
            // TODO: Pass the error message back to view
            log.error("Cannot get all vehicle types", e);
        }

        return vehicleTypes;
    }

    public String editVehicleType() {
        String vehicleTypeIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("vehicleTypeId");
        try {
            Long vehicleTypeId = Long.parseLong(vehicleTypeIdString);
            this.vehicleType = this.vehicleTypeManagerBean.getVehicleType(vehicleTypeId);
            if (this.vehicleType == null) {
                // TODO: Show error message
                return "listVehicleTypes";
            } else {
                return "editVehicleType";
            }
        } catch (VehicleTypeManagerException ex) {
            Logger.getLogger(VehicleTypeJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            // TODO: Pass the error message back to view
            return "listVehicles";
        }
    }

    public String saveVehicleType() {
        try {
            this.vehicleTypeManagerBean.editVehicleType(this.vehicleType);
            // TODO: Pass the success message back to view
            return "listVehicleTypes";
        } catch (VehicleTypeManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            // TODO: Pass the error message back to view
            return "editVehicleType";
        }
    }

    public String deleteVehicleType() {
        String vehicleTypeIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("vehicleTypeId");
        try {
            Long vehicleTypeId = Long.parseLong(vehicleTypeIdString);
            this.vehicleTypeManagerBean.deleteVehicleType(vehicleTypeId);
            // TODO: Display the success message
            return "listVehicleTypes";
        } catch (VehicleTypeManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            // TODO: Pass the error message back to view
            return "listVehicles";
        }
    }
}